﻿using LumenWorks.Framework.IO.Csv;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utilities.IO.CSV
{
    public class CsvHelper
    {
        private DataTable _dtDetails;
        private string _filePath;
        /// <summary>
        /// 存放读取到的CSV文件中的数据
        /// </summary>
        public DataTable DtDetails
        {
            get
            {
                return _dtDetails;
            }
        }
        /// <summary>
        /// 需要读取的文件路径
        /// </summary>
        public string FilePath
        {
            get
            {
                return _filePath;
            }
        }

        public CsvHelper(string filePath)
        {
            _filePath = filePath;
            _dtDetails = new DataTable();
        }

        /// <summary>
        /// 获取CSV文件信息
        /// </summary>
        /// <returns>文件内数据</returns>
        public DataTable GetCSVStream()
        {
            FileStream fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bytes = new byte[fileStream.Length];
            fileStream.Read(bytes, 0, bytes.Length);
            fileStream.Close();
            Stream stream = new MemoryStream(bytes);
            _dtDetails = GetCSVToDatable(stream);
            return _dtDetails;
        }


        /// <summary>
        /// 将csv文件转换成DataTable
        /// </summary>
        /// <param name="stream">流</param>
        /// <returns></returns>
        private DataTable GetCSVToDatable(Stream stream)
        {
            try
            {
                using (stream)
                {
                    //编码格式要选对，否则会乱码
                    using (StreamReader input = new StreamReader(stream, Encoding.GetEncoding("GBK")))
                    {
                        using (CsvReader csv = new CsvReader(input, true))
                        {
                            DataTable dt = new DataTable();
                            //第一行字段数量
                            int columnCount = csv.FieldCount;
                            //标题数组
                            string[] headers = csv.GetFieldHeaders();
                            //循环添加标题行
                            for (int i = 0; i < columnCount; i++)
                            {
                                dt.Columns.Add(headers[i]);
                            }
                            //循环添加列数据
                            while (csv.ReadNextRecord())
                            {
                                DataRow dr = dt.NewRow();
                                for (int i = 0; i < columnCount; i++)
                                {
                                    if (!string.IsNullOrWhiteSpace(csv[i]))
                                    {
                                        dr[i] = csv[i];
                                    }
                                }
                                dt.Rows.Add(dr);
                            }
                            return dt;
                        }
                    }
                }
            }
            catch
            {
                return new DataTable();
            }
        }

    }
}
